1 //Este código produce TLE
2 //Robado de http://online-judge.uva.es/board/viewtopic.php?t=27307&start=15
19 inline int value ( char ch
){
20 if ( isalpha ( ch
) ) return 10 + ch
- 'A';
25 while ( cin
>> input
){
26 if ( !strcmp ( input
, "#") ) break ;
28 int n
= strlen ( input
) ;
29 sort ( input
, input
+ n
) ;
30 for (register int mask
= 0 ; mask
< (1<<n
) ; mask
++ ){
31 for ( int mod
= 0 ; mod
< 5 ; mod
++ ){
32 if ( mask
== 0 ) dp
[mask
][mod
] = 0 ;
36 for ( int i
=0 ; i
<n
; i
++ ){
37 if ( mask
& ( 1<<i
) ){
39 int cur
= value ( input
[i
] );
40 if ( prev
== cur
) continue ;
42 next
= ( 500 + mod
- cur
) % 5;
43 if ( next
== 0 ) dp
[mask
][mod
] ++ ;
44 dp
[mask
][mod
] += dp
[mask
- (1<<i
)][next
];
48 if ( mask
== (1<<n
) - 1 && mod
== 0 )
52 cout
<< dp
[(1<<n
)-1][0] << endl
;